/*
 * Wazuh Vulnerability scanner
 * Copyright (C) 2015, Wazuh Inc.
 * March 25, 2023.
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License (version 2) as published by the FSF - Free Software
 * Foundation.
 */

#include "vulnerabilityScanner.hpp"
#include "cjsonSmartDeleter.hpp"
#include "loggerHelper.h"
#include "vulnerabilityScannerFacade.hpp"
#include "vulnerability_scanner.h"
#include <string>

// TODO: Remove LCOV flags once the implementation of the 'Indexer Connector' module is completed
// LCOV_EXCL_START
void VulnerabilityScanner::start(
    const std::function<void(
        const int, const std::string&, const std::string&, const int, const std::string&, const std::string&, va_list)>&
        logFunction,
    const nlohmann::json& configuration,
    const bool noWaitToStop,
    const bool reloadGlobalMapsStartup,
    const bool initContentUpdater)
{
    VulnerabilityScannerFacade::instance().start(
        logFunction, configuration, noWaitToStop, reloadGlobalMapsStartup, initContentUpdater);
}

void VulnerabilityScanner::stop()
{
    VulnerabilityScannerFacade::instance().stop();
}

#ifdef __cplusplus
extern "C"
{
#endif
    void vulnerability_scanner_start(full_log_fnc_t callbackLog, const cJSON* configuration)
    {
        nlohmann::json configurationNlohmann;
        if (configuration)
        {
            const std::unique_ptr<char, CJsonSmartFree> spJsonBytes {cJSON_Print(configuration)};
            configurationNlohmann = nlohmann::json::parse(spJsonBytes.get());
        }

        VulnerabilityScanner::instance().start(
            [callbackLog](const int logLevel,
                          const std::string& tag,
                          const std::string& file,
                          const int line,
                          const std::string& func,
                          const std::string& logMessage,
                          va_list args)
            { callbackLog(logLevel, tag.c_str(), file.c_str(), line, func.c_str(), logMessage.c_str(), args); },
            configurationNlohmann);
    }

    void vulnerability_scanner_stop()
    {
        VulnerabilityScanner::instance().stop();
    }

    bool vulnerability_scanner_policy_change_event(const char* data)
    {
        return true;
    }
#ifdef __cplusplus
}
#endif
// LCOV_EXCL_STOP
